package com.app.couponsharelife.ui.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.ui.main.home.good.GoodDetailActivity
import kotlinx.android.synthetic.main.choiceness_rvitm.view.*
import java.text.DecimalFormat

/**
 *Created by xct on 2019/1/9
 *Description:
 */
class GoodsSearchAdapter(private var context: Context,private var datas:List<TaobaoGoodBean>,private var goodsType:String):RecyclerView.Adapter<GoodsSearchAdapter.MyHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val view= LayoutInflater.from(parent.context).inflate(R.layout.choiceness_rvitm,parent,false)
        return MyHolder(view)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        if (datas.isNotEmpty()){
            if (position%2==0){
                holder.viewLeft!!.visibility=View.VISIBLE
                holder.viewRight!!.visibility=View.GONE
            }else{
                holder.viewRight!!.visibility=View.VISIBLE
                holder.viewLeft!!.visibility=View.GONE
            }
            val df = DecimalFormat("#0.00")
            val taobaoGoodBean=datas[position]
            holder.goodsTitle!!.text = taobaoGoodBean.title
            Glide.with(context).asBitmap()
                    .apply(RequestOptions().error(R.drawable.ic_app_place_holder3))
                    .load(taobaoGoodBean.pict_url)
                    .into(holder.goodsImag!!)
            holder.oldpriceType!!.text=taobaoGoodBean.goodsSource+"价"
            if (taobaoGoodBean.has_coupon){
                holder.goodsTextQh!!.text="券后"
                holder.goodsCoupon!!.text=String.format("¥%s", taobaoGoodBean.coupon_price)
                holder.couponLL!!.visibility=View.VISIBLE
                holder.goodsOldPrice!!.text=taobaoGoodBean.zk_final_price
            }else{
                holder.goodsTextQh!!.text="抢"
                holder.couponLL!!.visibility=View.INVISIBLE
                holder.goodsOldPrice!!.text=taobaoGoodBean.reserve_price
            }
            holder.goodsType!!.setImageResource(taobaoGoodBean.userType)
            holder.goodsAward!!.text="￥${taobaoGoodBean.vip_commission}"
            holder.goodsPrice!!.text="￥${taobaoGoodBean.final_price}"
            holder.goodsSales!!.text=taobaoGoodBean.volume
            holder.shopName!!.text=taobaoGoodBean.shop_title
            holder.shopType!!.setImageResource(taobaoGoodBean.userType)
            holder.itemView.setOnClickListener {
                setIntent(taobaoGoodBean)
            }
        }
    }
    private fun setIntent(taobaoGoodBean:TaobaoGoodBean){
        val intent = Intent(context, GoodDetailActivity::class.java)
        intent.putExtra("good", taobaoGoodBean)
        if ("拼多多"==taobaoGoodBean.goodsSource){
            intent.putExtra("GoodsType", "PDD")
        }else if ("京东"==taobaoGoodBean.goodsSource){
            intent.putExtra("GoodsType", "JD")
        }
        context.startActivity(intent)
    }
    inner class MyHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        var goodsTitle: TextView?=null
        var viewLeft:View?=null
        var viewRight:View?=null
        var goodsPrice:TextView?=null
        var goodsImag: ImageView?=null
        var goodsOldPrice:TextView?=null
        var goodsCoupon:TextView?=null
        var couponLL: LinearLayout?=null
        var goodsAward:TextView?=null
        var goodsSales:TextView?=null
        var goodsType:ImageView?=null
        var oldpriceType:TextView?=null
        var goodsTextQh:TextView?=null
        var shopName:TextView?=null
        var shopType:ImageView?=null
        init {
            goodsTitle=itemView.choiceness_rvitm_title
            viewLeft=itemView.choiceness_rvitm_left
            viewRight=itemView.choiceness_rvitm_right
            goodsImag=itemView.choiceness_rvitm_img
            goodsOldPrice=itemView.choiceness_rvitm_oldprice
            goodsCoupon=itemView.choiceness_rvitm_quanprice
            couponLL=itemView.choiceness_rvitm_llq1
            goodsAward=itemView.choiceness_rvitm_jiangprice
            goodsSales=itemView.choiceness_rvitm_yuenumber
            goodsType=itemView.choiceness_rvitm_imgtype
            goodsPrice=itemView.choiceness_rvitm_price
            oldpriceType=itemView.choiceness_rvitm_oldpricetype
            goodsTextQh=itemView.choiceness_rvitm_qh
            shopName=itemView.choiceness_rvitm_tvShopName
            shopType=itemView.choiceness_rvitm_ivGoodsType
            goodsOldPrice!!.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
        }
    }
}